<?php
//展示留言
    include "db.php";

//    根据页数设置数据
    $page = isset($_GET['page']) ? intval($_GET['page']):1;
    $start = ($page-1)*5;


//    取出数据库对应的5条数据
    $sql = "select * from message order by id desc limit $start,5";
//    不是从0到5，是0开始5条
    $result = $conn->query($sql);
    $messages = [];
//    var_dump($result);

    while($row = $result->fetch_assoc()){
        $messages [] = $row;
    }

//    echo '<pre>';
//    var_dump($messages);


//    取出表里数据的个数,用于计算页数
    $sql = "select count(*) as total from message";
    $result = $conn->query($sql);
    $row2 = $result->fetch_assoc();
    $total = $row2['total'];
    $pages = ceil($total/5);

    $conn->close();
?>

<div style="text-align: center">
<h2>留言一览</h2>
<table border style=" margin: auto" >
    <tr>
        <th>ID</th>
        <th>留言人</th>
        <th>留言内容</th>
        <th>留言时间</th>
    </tr>
    <?php foreach ($messages as $msg):?>
    <tr>
        <td><?php echo $msg['id']; ?></td>
        <td><?php echo $msg['name']; ?></td>
        <td><?php echo $msg['content']; ?></td>
        <td><?php echo $msg['time']; ?></td>
    </tr>
    <?php endforeach; ?>


<!--    输出页码-->
    <?php for ($i = 1;$i<=$pages; $i++):?>
        <a href="?page=<?php echo $i; ?>"><?php echo $i; ?>   </a>
    <?php endfor; ?>
</table>
</div>



